﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.ObjectModel;

namespace YourFileWatcher.Models
{
    /// <summary>
    /// 一个用于监视目录改变的操作对象
    /// </summary>
    public class WatcherItem : PropertyChangedBase
    {
        /// <summary>
        /// 开启一个监视器
        /// </summary>
        /// <param name="path">要监视的目录</param>
        /// <param name="pLogs">存放日志的容器</param>
        public WatcherItem(string path, ObservableCollection<LogInfoItem> pLogs)
        {
            this.watcherPath = path;
            this.logs = pLogs;

            StartWatcher();
        }

        private string watcherPath;
        ObservableCollection<LogInfoItem> logs;
        FileSystemWatcher watcher;

        /// <summary>
        /// 只读，获取本监视器捕获的路径
        /// </summary>
        public string WatcherPath
        {
            get { return watcherPath; }
        }
        public bool? IsWork
        {
            get
            {
                bool? result = null;
                if (null != watcher)
                {
                    result = watcher.EnableRaisingEvents;
                }
                return result;
            }
            set
            {
                if (value == true)
                {
                    StartWatcher();
                }
                else
                {
                    StopWatcher();
                }
                RaisePropertyChanged("IsWork");
            }
        }



        /// <summary>
        /// 开启监视
        /// </summary>
        public void StartWatcher()
        {
            if (!Directory.Exists(watcherPath)) throw new FileNotFoundException("要监视的目录不存在");


            if (null == watcher)
            {
                watcher = new FileSystemWatcher();
                watcher.Path = watcherPath;
                watcher.IncludeSubdirectories = true;
                watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

                //添加监视器
                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created += new FileSystemEventHandler(OnChanged);
                watcher.Deleted += new FileSystemEventHandler(OnChanged);
                watcher.Renamed += new RenamedEventHandler(OnRenamed);
            }

            //开始监视
            watcher.EnableRaisingEvents = true;
        }

        /// <summary>
        /// 停止监视
        /// </summary>
        public void StopWatcher()
        {
            if (null != watcher)
            {
                watcher.EnableRaisingEvents = false;
            }
        }


        private void OnChanged(object source, FileSystemEventArgs e)
        {
            var log1 = new LogInfoItem();
            log1.Time = DateTime.Now;
            log1.Name = Path.GetFileName(e.Name);
            log1.FullName = e.FullPath;
            log1.ChangeType = e.ChangeType;

            if (null != logs)
            {
                App.Current.Dispatcher.Invoke(new Action(() => logs.Insert(0, log1)));
            }
        }

        private void OnRenamed(object source, RenamedEventArgs e)
        {
            var log1 = new LogInfoItem();
            log1.Time = DateTime.Now;
            log1.Name = Path.GetFileName(e.Name);
            log1.FullName = e.FullPath;
            log1.OldFullName = e.OldFullPath;
            log1.ChangeType = e.ChangeType;

            if (null != logs)
            {
                App.Current.Dispatcher.Invoke(new Action(() => logs.Insert(0, log1)));
            }
        }

    }
}
